<template>
  <div class="min-h-screen bg-white">
    <div v-if="post" class="max-w-4xl mx-auto px-4 sm:px-6 lg:px-8 py-12">
      <!-- Article Header -->
      <header class="mb-12">
        <div class="mb-4">
          <router-link to="/blog" class="text-sm text-gray-500 hover:text-primary-600 transition-colors duration-200">
            ← 返回博客
          </router-link>
        </div>

        <h1 class="text-4xl md:text-5xl font-bold text-gray-900 mb-8 leading-tight">
          {{ post.title }}
        </h1>
      </header>

      <!-- Article Content -->
      <article class="prose prose-lg max-w-none">
        <div v-html="parsedContent"></div>
      </article>

      <!-- Reading Stats -->
      <div class="mt-12 pt-8 border-t border-gray-100">
        <div class="flex items-center justify-end">
          <div class="bg-gray-50 rounded-full px-6 py-3 shadow-sm border border-gray-200">
            <div class="flex items-center text-gray-600">
              <div class="w-2 h-2 bg-primary-500 rounded-full mr-3 animate-pulse"></div>
              <span class="text-sm font-medium">{{ post.views }} 次阅读</span>
            </div>
          </div>
        </div>
      </div>


    </div>

    <!-- Loading State -->
    <div v-else class="min-h-screen flex items-center justify-center">
      <div class="text-center">
        <div class="animate-spin rounded-full h-12 w-12 border-b-2 border-primary-600 mx-auto mb-4"></div>
        <p class="text-gray-600">加载中...</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute } from 'vue-router'
import { getArticleById } from '../services/articleService'
import { marked } from 'marked'

const route = useRoute()
const postId = parseInt(route.params.id)

const post = ref(null)
const loading = ref(true)
const parsedContent = ref('')

// 加载文章数据
const loadPost = async () => {
  try {
    loading.value = true
    const articleData = await getArticleById(postId)
    
    if (articleData) {
      post.value = articleData
      // 使用marked解析Markdown内容
      parsedContent.value = marked(articleData.content)
    }
  } catch (error) {
    console.error('加载文章失败:', error)
  } finally {
    loading.value = false
  }
}

// 组件挂载时加载文章
onMounted(() => {
  loadPost()
})
</script>

<style scoped>
.line-clamp-2 {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 代码块样式优化 */
:deep(.prose pre) {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  border: 1px solid #334155;
  border-radius: 12px;
  padding: 1.5rem;
  margin: 1.5rem 0;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
  position: relative;
  overflow-x: auto;
}

:deep(.prose pre::before) {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #3b82f6, #8b5cf6, #06b6d4, #10b981);
  border-radius: 12px 12px 0 0;
}

:deep(.prose pre code) {
  background: transparent;
  color: #e2e8f0;
  font-family: 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
  font-size: 0.875rem;
  line-height: 1.6;
  padding: 0;
  border-radius: 0;
  white-space: pre;
}

/* 行内代码样式 */
:deep(.prose code:not(pre code)) {
  background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
  color: #dc2626;
  padding: 0.25rem 0.5rem;
  border-radius: 6px;
  font-family: 'Fira Code', 'JetBrains Mono', 'Consolas', monospace;
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid #cbd5e1;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* 代码块标题 */
:deep(.prose pre[data-language]::before) {
  content: attr(data-language);
  position: absolute;
  top: -1px;
  right: 1rem;
  background: #3b82f6;
  color: white;
  padding: 0.25rem 0.75rem;
  font-size: 0.75rem;
  font-weight: 600;
  border-radius: 0 0 6px 6px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* 代码块滚动条样式 */
:deep(.prose pre::-webkit-scrollbar) {
  height: 8px;
}

:deep(.prose pre::-webkit-scrollbar-track) {
  background: #1e293b;
  border-radius: 4px;
}

:deep(.prose pre::-webkit-scrollbar-thumb) {
  background: #475569;
  border-radius: 4px;
}

:deep(.prose pre::-webkit-scrollbar-thumb:hover) {
  background: #64748b;
}

/* 代码高亮优化 */
:deep(.prose pre .token.comment) {
  color: #64748b;
  font-style: italic;
}

:deep(.prose pre .token.keyword) {
  color: #c084fc;
  font-weight: 600;
}

:deep(.prose pre .token.string) {
  color: #34d399;
}

:deep(.prose pre .token.number) {
  color: #fbbf24;
}

:deep(.prose pre .token.function) {
  color: #60a5fa;
}

:deep(.prose pre .token.class-name) {
  color: #f472b6;
}

:deep(.prose pre .token.operator) {
  color: #fb7185;
}

/* 代码块悬停效果 */
:deep(.prose pre:hover) {
  transform: translateY(-2px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
  transition: all 0.3s ease;
}

/* 复制按钮样式 */
:deep(.prose pre .copy-button) {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: rgba(59, 130, 246, 0.8);
  color: white;
  border: none;
  padding: 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.75rem;
  opacity: 0;
  transition: all 0.3s ease;
}

:deep(.prose pre:hover .copy-button) {
  opacity: 1;
}

:deep(.prose pre .copy-button:hover) {
  background: rgba(59, 130, 246, 1);
  transform: scale(1.05);
}

/* 响应式优化 */
@media (max-width: 768px) {
  :deep(.prose pre) {
    padding: 1rem;
    margin: 1rem 0;
    border-radius: 8px;
  }
  
  :deep(.prose pre code) {
    font-size: 0.8rem;
  }
}
</style>
